package meijia.bean;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import meijia.model.UserInfo;
import meijia.utils.DBConn;

public class UserBean {
	private DBConn db;
	public void addNewUser(UserInfo userinfo){
		java.util.Date dt = new java.util.Date();
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentTime = sdf.format(dt);
		String sql = "insert into userinfo(cardNo,name,money,phone,address,note,date) value('" + userinfo.getCardNo() +  "','" 
				+ userinfo.getName() + "','"+ userinfo.getMoney() +"','"
				+ userinfo.getPhone() +"','"+userinfo.getAddress()+"','"+ userinfo.getNote() +"','" + currentTime +"')";
		db = new DBConn();
		db.doInsert(sql);
	}

	public List<UserInfo> getAllUsers() throws SQLException{
		String sql = "select * from userinfo";
		db = new DBConn();
		ResultSet rs = db.doSelect(sql);
		UserInfo userInfo;
		List<UserInfo> list = new ArrayList<UserInfo>();
		while(rs.next()){
			userInfo = new UserInfo();
			userInfo.setCardNo(rs.getString("cardNo"));
			userInfo.setName(rs.getString("name"));
			userInfo.setAddress(rs.getString("address"));
			userInfo.setId(rs.getLong("id"));
			userInfo.setNote(rs.getString("name"));
			userInfo.setPhone(rs.getString("phone"));
			userInfo.setDate(rs.getDate("date"));
			userInfo.setMoney(rs.getDouble("money"));
			list.add(userInfo);
		}
		return list;
	}
	
}
